const app = getApp()
var common = require('../../../static/utils/common.util.js');
var tagStyle00 = "background: #efefef;\n  color: #555;\n  padding: 0 5px;\n  border-radius: 10px;\n  text-align: center;\n  height: 25px;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n  line-height: 20px;\n";

var selectStyle = "background: #54d09f;\n  color: #fff;\n  padding: 0 5px;\n  border-radius: 10px;\n  text-align: center;\n  height: 25px;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n  line-height: 20px;\n";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: "",
    assoName: "",
    collegeName: "",
    assoType: "",
    createTime: "",
    studentName: "",
    stjj: "",
    stxq: null,
    stbm: [],
    stzps: [],
    swiperCurrent: 0,
    depList: [],
    depDesc: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      schoolName,
      collegeName,
      assoName,
      assoType,
      stjj,
      stxq
    } = options
    var studentName = app.globalData.studentInfo.studentName
    var stzps = JSON.parse(options.stzps);
    var depList = JSON.parse(options.depList)
    var createTime = common.getNowFormatDate();
    console.log(createTime)
    this.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl,
      schoolName: schoolName,
      collegeName: collegeName,
      assoName: assoName,
      assoType: assoType,
      createTime: createTime,
      studentName: studentName,
      stjj: stjj,
      stxq: stxq,
      stzps: stzps,
      stbm: options.depList,
    })
    var that = this
    var lists = []
    if(depList!=null){
      depList.forEach(function (item, index) {
        lists.push({
          text: item.depName,
          depDesc: item.depDesc,
          tagStyle: tagStyle00,
          tagSelectedStyle: selectStyle,
          checked: false
        })
      })
    }
    if (lists.length != 0) {
      lists[0].checked = true
      that.setData({
        depList: lists,
        depDesc: lists[0].depDesc
      })
    }
  },
  singleTap: function singleTap(e) {
    var opt = e.detail.index;
    this.data.depList.forEach(function(item, index) {
      item.checked = index === opt;
    });
    this.setData({
      depList: this.data.depList,
      depDesc: this.data.depList[opt].depDesc
    });
  },
  previewImage: function() {
    wx.previewImage({
      current: this.data.stzps, // 当前显示图片的http链接
      urls: this.data.stzps // 需要预览的图片http链接列表
    })
  },
  swiperChange: function(e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  sendButton: function() {
    var that = this
    wx.showModal({
      title: '提示',
      content: "确认提交",
      success: function(res) {
        if (res.confirm) {
          that.sendZpxx();
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  sendZpxx: function() {
    var that = this
    var data = that.data
    console.log(data.stbm)
    console.log(data.stzps)
    wx.showLoading({
      title: '上传中',
    })
    var fileIDs=[]
    for (var i = 0; i < data.stzps.length; i++) {
      const filePath = data.stzps[i]
      const cloudPath = 'zpxxImgs/' +
        data.schoolName + "/" +
        data.collegeName + "/" +
        data.assoName + "/" +
        i + filePath.match(/\.[^.]+?$/)[0]
      console.log(cloudPath)
      wx.cloud.uploadFile({
        cloudPath,
        filePath,
        success: res => {
          console.log('[上传文件] 成功：', res)
          console.log('[上传文件] cloudPath：', cloudPath)
          console.log('[上传文件] filePath', filePath)
          fileIDs.push(res.fileID)          
          console.log(fileIDs)
        },
        complete:res =>{
          if (data.stzps.length == fileIDs.length){
            wx.hideLoading()
            console.log(JSON.stringify(fileIDs))
            wx.request({
              url: app.globalData.url + '/zpxx/save',
              data: {
                avatarUrl: data.avatarUrl,
                assoName: data.assoName,
                assoType: data.assoType,
                collegeName: data.collegeName,
                schoolName: data.schoolName,
                studentName:data.studentName,
                stjj: data.stjj,
                stxq: data.stxq,
                depImgs: JSON.stringify(fileIDs),
                depList: data.stbm,
              },
              success:res=>{
                if(res.data.code==1){
                  wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    success: function (res) {
                      if (res.confirm) {
                        wx.switchTab({
                          url: '../index/index',
                        })
                        console.log('用户点击确定')
                      } else {
                        console.log('用户点击取消')
                      }
                    }
                  })
                }
              }
            })
          }
        }      
      })
    }      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})